#include "shadow.h"

extern int G_W, G_H;

SHADOW::SHADOW(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject ()
{

    shot = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_7/shot.ogg",this);
    QTimer::singleShot(4000, shot,SLOT(Play_Now_Slot()));

    qsrand(QTime::currentTime().second());

    point=30;

    border = cash->cash_poly->value("Res/weapons/Shadow_1/Shadow.poly");
    im = cash->cash_pixmap->value("Res/weapons/Shadow_1/Shadow.png");

    QPoint start( 0+qrand()%((G_W-im->width())-0+1),G_H+20);

    this->setPos(start);

    anim_rocket = new QGraphicsItemAnimation();

    rocket_time_line = new QTimeLine(1000);                                             // Скорость перемещения обьекта по траектории
    rocket_time_line->setEasingCurve( QEasingCurve::Type(1));

    rocket_time_line->setFrameRange(0, 1);

    anim_rocket->setTimeLine(rocket_time_line);
    anim_rocket->setItem(this);

    anim_rocket->setPosAt(0,start);
    anim_rocket->setPosAt(1,QPoint(start.x(),(im->height())*(-1) ) );

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(rocket_time_line,SIGNAL(finished()),SLOT(deleteLater()));

}

QPainterPath SHADOW::shape() const
{

    return *border;

}

QRectF SHADOW::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );


}

void SHADOW::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

void SHADOW::Start()
{

    rocket_time_line->start();

}

SHADOW::~SHADOW()
{
}

int SHADOW::type() const
{

    return point;

}


void SHADOW::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(rocket_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            //shot->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}
